import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/jsonserialize/active/data.dart';
import 'package:im_flutter_sdk_example/jsonserialize/expert/home_data.dart';
import 'package:im_flutter_sdk_example/jsonserialize/need/data.dart';
import 'package:im_flutter_sdk_example/jsonserialize/project/data.dart';
import 'package:im_flutter_sdk_example/jsonserialize/recommend/news.dart';
import 'package:im_flutter_sdk_example/jsonserialize/serve/data.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';

class HomeModel with ChangeNotifier {
  List<ProjectItem> _projectData = [];
  List<HomeExpertData> expertData = [];
  List<ServeData> serviceData = [];
  List<NeedData> demandData = [];
  List<ActiveData> activityData = [];
  List<NewsData> projectNews = [];
  List<NewsData> expertNews = [];
  List<NewsData> serviceNews = [];
  List<NewsData> demandNews = [];
  List<NewsData> activityNews = [];

  List<NewsData> recommentMoreNews = [];

  get projectData => _projectData;

  void getData() async {
    var res = await G.req.home.recommendList();
    var sections = res.data['data']['sections'];
    sections.forEach((e) {
      if (e['title'] == 'project') {
        e['data'].forEach((e) {
          ProjectItem project = ProjectItem.fromJson(e);
          _projectData.add(project);
        });
        e['news'].forEach((e) {
          NewsData pNews = NewsData.fromJson(e);
          projectNews.add(pNews);
        });
      }
      if (e['title'] == 'expert') {
        e['data'].forEach((e) {
          HomeExpertData expert = HomeExpertData.fromJson(e);
          expertData.add(expert);
        });
        e['news'].forEach((e) {
          NewsData eNews = NewsData.fromJson(e);
          expertNews.add(eNews);
        });
      }
      if (e['title'] == 'service') {
        e['data'].forEach((e) {
          ServeData serve = ServeData.fromJson(e);
          serviceData.add(serve);
        });
        e['news'].forEach((e) {
          NewsData sNews = NewsData.fromJson(e);
          serviceNews.add(sNews);
        });
      }
      if (e['title'] == 'demand') {
        e['data'].forEach((e) {
          NeedData demand = NeedData.fromJson(e);
          demandData.add(demand);
        });
        e['news'].forEach((e) {
          NewsData dNews = NewsData.fromJson(e);
          demandNews.add(dNews);
        });
      }
      if (e['title'] == 'activity') {
        e['data'].forEach((e) {
          ActiveData active = ActiveData.fromJson(e);
          activityData.add(active);
        });
        e['news'].forEach((e) {
          NewsData aNews = NewsData.fromJson(e);
          activityNews.add(aNews);
        });
      }
    });
    notifyListeners();
  }
}
